package pl.diagnoser.server.persistance.map;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import pl.diagnoser.client.dto.ObjectsDTO;
import pl.diagnoser.client.tools.MD5;

@Entity
@Table(name = "T_OBJECT")
public class Objects implements Serializable {

	private static final long serialVersionUID = 1L;
	private Long id;
	private String name;
	private String nameMd5;
	private Date date;
	private App app;
	private Set<ObjectAttributes> objectAttributes = new HashSet<ObjectAttributes>(0);
	
	public Objects() {};
	public Objects( String name, String date, App app ) {
		this.name = name;
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		try {
			this.app = app;
			this.nameMd5 = MD5.getInstance().makeMD5(name);
			if(date != null)
				this.date = formatter.parse(date);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	
	public Objects( String name, String nameMd5, Set<ObjectAttributes> objectAttributes ) {
		this.name = name;
		this.nameMd5 = nameMd5;
		this.objectAttributes = objectAttributes;
	}
	
	public Objects( ObjectsDTO objects ) {
		this.id = objects.getId();
		this.name = objects.getName();
	}
	
	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column( name="OBJ_ID", unique = true, nullable = false )
	public Long getId() {
		return this.id;
	}
	
	public void setId( Long id ) {
		this.id = id;
	}
	
	@Column( name="OBJ_NAME", unique = false, nullable = false, length = 255 )
	public String getName() {
		return name;
	}
	
	public void setName( String name ) {
		this.name = name;
	}
	
	@Column( name="OBJ_NAME_MD5", unique = true, nullable = false, length = 255 )
	public String getNameMd5() {
		return nameMd5;
	}
	
	public void setNameMd5(String nameMd5) {
		this.nameMd5 = nameMd5;
	}
	
	@Column( name="OBJ_DATE", unique = false, nullable = true )
	public Date getDate() {
		return date;
	}
	
	public void setDate(Date date) {
		this.date = date;
	}

	@OneToMany( mappedBy = "objects" )
	public Set<ObjectAttributes> getObjectAttributes() {
		return objectAttributes;
	}
	
	public void setObjectAttributes(Set<ObjectAttributes> objectAttributes) {
		this.objectAttributes = objectAttributes;
	}
	
	@ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="APP_ID")
	public App getApp() {
		return app;
	}
	public void setApp(App app) {
		this.app = app;
	}
}
